package com.coreform;
import com.coreform.model.Attribute;
import com.coreform.model.CoreForm;
import com.gdth.widget.dwonloadlist.BaseDownListTextView;

import android.content.Context;
import android.text.InputType;
import android.util.SparseArray;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

/** 
 * @author 作者 Arvin: 
 * @version 创建时间：2015年6月9日 下午3:34:48 
 * 类说明 
 */
public class CoreFormWidget extends LinearLayout{
	private Context mContext;
	
	private String CoreFormName;
	
	public CoreForm mCoreForm;
	
	public SparseArray<Object> mSparseArray;
	
	public CoreFormWidget(Context context, String path) {
		super(context);
		this.mContext = context;
		this.CoreFormName = path;
		
		initView();
	}
	
	private void initView(){
		mSparseArray = new SparseArray<Object>();
	}
	
	public void initCoreForm(){
		if (mCoreForm == null) {	
			mCoreForm = CoreFormUtil.xmlParseByElement(mContext, CoreFormName);
        }
		//初始化失败时返回
		if (mCoreForm == null)
			return;
		int lenhth = mCoreForm.getAttributeList().size();

    	for (int position = 0; position < lenhth; position++){
			//获取控件对象信息
			Attribute attribute = mCoreForm.getAttributeList().get(position);
        	
        	int id = attribute.getId();
            String name = attribute.getName();
            String type = attribute.getType();
            String control = attribute.getControl();
            String initVal = attribute.getInit();
            String defaultval = attribute.getDefaultval();
            String unit = attribute.getUnit();
            String titleName = name;
            
            if (!unit.equals("")) titleName += "(" + unit + ")";
            
			
			//线性容器，垂直居中
			LinearLayout linear = new LinearLayout(this.mContext);
			linear.setOrientation(0); //HORIZONTAL(0)、VERTICAL(1)
			
			//label控件
			TextView text = new TextView(this.mContext);
	        text.setText(titleName);
	        text.setGravity(Gravity.LEFT);
	        linear.addView(text);
	        
	        //value控件
            if (control.equals("text")){
            	EditText object = new EditText(this.mContext); 
            	object.setId(id); 
            	object.setGravity(Gravity.CENTER_VERTICAL);
                
                //设置输入法格式
                if (type.equals("int")) object.setInputType(InputType.TYPE_CLASS_NUMBER);
                if (type.equals("decimal")) object.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

                if (defaultval.equals("")) object.setHint(initVal);
                else object.setText(defaultval);
                
                linear.addView(object);
                
                mSparseArray.append(position, object);
            }
            else if (control.equals("uidatetime")){
            	TextView object = new TextView(this.mContext); 
            	object.setId(id);
            	object.setGravity(Gravity.CENTER_VERTICAL);
            	
                linear.addView(object);
                
                mSparseArray.append(position, object);
            }
            
            else if (control.equals("spinner")){
            	String dataSource = attribute.getDatasource();
            	BaseDownListTextView object = CoreFormUtil.getDrawDownListTextViewByResources(this.mContext, defaultval, initVal, dataSource);
            	
                linear.addView(object);
                mSparseArray.append(position, object);
            }
            else if (control.equals("drawdownlist")){
            	String dataSource = attribute.getDatasource();
            	
            	BaseDownListTextView object = CoreFormUtil.getDrawDownListTextView(mContext, defaultval, initVal, dataSource);

                linear.addView(object);
                
                mSparseArray.append(position, object);
            }
            
            this.addView(linear);
    	}
	}
	
	/**
	 * 清空数据
	 */
	public void removeAll(){
		mSparseArray.clear();
    	this.removeAllViews();
	}
}
